package com.niit.az;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.SQLException;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    EditText edit_account;
    EditText edit_password;
    Button btn_login;
    Button btn_register;

    StudentOP studentOP=new StudentOP(this);

    private systemReceiver systemReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit_account = findViewById(R.id.edit_account);
        edit_password = findViewById(R.id.edit_password);
        btn_login=findViewById(R.id.btn_login);
        btn_register=findViewById(R.id.btn_register);
        edit_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        systemReceiver = new systemReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(systemReceiver,filter);

        try{
            studentOP.open();
        }catch (SQLException e){
            e.printStackTrace();
        }

        btn_login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String str_account = edit_account.getText().toString();
                String str_password = edit_password.getText().toString();
                Boolean stud = studentOP.login(str_account,str_password);
                if(stud){
                    Intent myIntent=new Intent(MainActivity.this,Welcome.class);
                    startActivity(myIntent);
                }else{
               Toast.makeText(MainActivity.this,"账号或密码错误，请重新输入！！！",Toast.LENGTH_LONG).show();
            }
            }
        });

        btn_register.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent myIntent = new Intent(MainActivity.this,Register.class);
                startActivity(myIntent);
                Toast.makeText(MainActivity.this,"您点击了注册按钮,为您跳转至注册页面！",Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (systemReceiver!=null){
            unregisterReceiver(systemReceiver);
        }
    }

    public class systemReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case Intent.ACTION_BOOT_COMPLETED:
                    Log.e("TAG","手机开机了");
                    break;
                case  Intent.ACTION_SHUTDOWN:
                    Log.e("TAG","手机关机了");
                    break;
                case Intent.ACTION_SCREEN_ON:
                    Log.e("TAG","亮屏");
                    Toast.makeText(context,"您收到的广播为：您的手机亮屏啦，可以开始使用啦！",Toast.LENGTH_LONG).show();
                    break;
                case Intent.ACTION_SCREEN_OFF:
                    Log.e("TAG","息屏");
                    break;
                case Intent.ACTION_USER_PRESENT:
                    Log.e("TAG","手机解锁");
                    break;

            }

        }
    }
}